package net.neptech.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import net.neptech.api.SystemApi;
import net.neptech.dto.sys.StaffDTO;
import net.neptech.model.dto.TaskParamDto;
import net.neptech.model.dto.TaskResultDto;
import net.neptech.model.dto.UploadValue;
import net.neptech.enums.workflow.OperTypeEnum;
import net.neptech.model.workflow.InstanceOperRecord;
import net.neptech.model.workflow.InstanceRecord;
import net.neptech.repository.InstanceOperRecordRepo;
import net.neptech.service.FlowableService;
import net.neptech.service.InstanceOperRecordService;
import net.neptech.service.InstanceRecordService;
import net.neptech.util.JsonUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author Saya
 * @Date 2025/8/20 12:04
 * @Description TODO
 **/
@Service
@RequiredArgsConstructor
public class InstanceOperRecordServiceImpl implements InstanceOperRecordService {
    private final InstanceRecordService instanceRecordService;
    private final InstanceOperRecordRepo instanceOperRecordRepo;
    private final FlowableService flowableService;
    private final SystemApi systemApi;

    /**
     * 保存记录
     */
    @Override
    public TaskResultDto saveRecord(String userId, TaskParamDto taskParamDto, String operType, String desc) {
        StaffDTO staff = systemApi.getStaffByUserId(userId).getData();
        InstanceOperRecord instanceOperRecordDto = new InstanceOperRecord();
        instanceOperRecordDto.setUserId(staff.getId());
        instanceOperRecordDto.setProcessInstanceId(taskParamDto.getProcessInstanceId());
        instanceOperRecordDto.setComment(taskParamDto.getApproveDesc());
        TaskResultDto taskResultDto = flowableService.queryTask(taskParamDto.getTaskId(), null);
        instanceOperRecordDto.setNodeId(taskResultDto.getNodeId());
        instanceOperRecordDto.setNodeName(taskResultDto.getNodeName());
        instanceOperRecordDto.setFlowId(taskResultDto.getFlowId());
        List<UploadValue> approveImageList = taskParamDto.getApproveImageList();
        List<UploadValue> approveFileList = taskParamDto.getApproveFileList();
        instanceOperRecordDto.setImageList(JsonUtil.toJSONString(approveImageList));
        instanceOperRecordDto.setFileList(JsonUtil.toJSONString(approveFileList));
        instanceOperRecordDto.setOperType(operType);
        instanceOperRecordDto.setOperDesc(StrUtil.format("{}[{}] / {} / {} / {} / {} / {} ", staff.getStaffName(), userId, taskResultDto.getNodeName(), DateUtil.formatDateTime(new Date()), desc, OperTypeEnum.getByValue(operType).getName(), StrUtil.blankToDefault(taskParamDto.getApproveDesc(), "")));
        instanceOperRecordRepo.save(instanceOperRecordDto);
        return taskResultDto;
    }

    /**
     * 撤销流程
     */
    @Override
    public void saveCancelProcessRecord(String userId, String instanceId) {
        InstanceRecord instanceRecord = instanceRecordService.getByInstanceId(instanceId);
        StaffDTO staff = systemApi.getStaffByUserId(userId).getData();
        InstanceOperRecord instanceOperRecordDto = new InstanceOperRecord();
        instanceOperRecordDto.setUserId(staff.getId());
        instanceOperRecordDto.setProcessInstanceId(instanceId);
        instanceOperRecordDto.setFlowId(instanceRecord.getFlowId());
        instanceOperRecordDto.setOperType(OperTypeEnum.CANCEL.getValue());
        instanceOperRecordDto.setOperDesc(StrUtil.format("{}[{}] / {} / {} / {} / {} / {}", staff.getStaffName(), userId, "", DateUtil.formatDateTime(new Date()), "撤销流程", OperTypeEnum.CANCEL.getName(), ""));
        instanceOperRecordRepo.save(instanceOperRecordDto);
    }

    /**
     * 发起流程
     */
    @Override
    public void saveStartProcessRecord(String userId, String processInstanceId, String flowId) {
        StaffDTO staff = systemApi.getStaffByUserId(userId).getData();
        InstanceOperRecord instanceOperRecordDto = new InstanceOperRecord();
        instanceOperRecordDto.setUserId(userId);
        instanceOperRecordDto.setProcessInstanceId(processInstanceId);
        instanceOperRecordDto.setFlowId(flowId);
        instanceOperRecordDto.setNodeName("开始");
        instanceOperRecordDto.setOperType(OperTypeEnum.START.getValue());
        instanceOperRecordDto.setOperDesc(StrUtil.format("{}[{}] / {} / {} / {} / {} / {}", staff.getStaffName(), userId, "开始节点", DateUtil.formatDateTime(new Date()), "发起流程", OperTypeEnum.START.getName(), ""));
        instanceOperRecordRepo.save(instanceOperRecordDto);
    }

    /**
     * 清理数据
     */
    @Override
    public void clearProcess(String uniqueId, List<String> flowIdList, List<Long> processIdList, String tenantId) {
        List<InstanceOperRecord> list = instanceOperRecordRepo.findByFlowIdIn(flowIdList);
        list.forEach(instanceOperRecord -> instanceOperRecord.setDeleted(true));
        instanceOperRecordRepo.saveAll(list);
    }
}
